/**
 * Created by TomSean on 2017/6/13.
 */
var ParkingLogManager = function ($parent) {
    this.$parent = $parent;
    this.sysUtils = new kendoUtils();
    this.init();
    return this;
}

ParkingLogManager.prototype = {
    init : function () {
        var $this = this;
        var fieldArray = $this.sysUtils.getBindFieldArray($this.$parent.find('.search-panel'));
        var functionArray = [
            {fName : 'find_data', fs : $this.findData},
            {fName : 'reset', fs : $this.reset}
        ];
        $this.models = new bindBaseModel(null, fieldArray, functionArray, null);
        $this.sysUtils.bindDoucment($this.models, $this.$parent, $this);
        $this.initTable();
        $this.findData();
    },
    reset : function (e) {
        var that = $(e.target).data("that");
        that.sysUtils.setBindFieldNull(that.models.bindFieldArray);
    },
    initTable : function () {
        var $this = this;
        var columns = [
            {
                width:30,
                headerTemplate : '<input type="checkbox" gridId="parking_lot_list" onclick="gridCheckAll(this)" />',
                template : '<input type="checkbox" name="owner_id" value="${ownerCode}" />'
            },
            {
                title : '车主编号',
                field : 'ownerCode'
            },
            {
                title : '车主姓名',
                field : 'ownerName'
            },
            {
                title : '车主电话',
                field : 'ownerPhone'
            },
            {
                title : '通讯地址',
                field : 'ownerAddress'
            },
            {
                title : '所属组织',
                field : 'organizationName'
            }
        ];
        $this.sysUtils.createGird($this.$parent.find('#parking_lot_list'), {
            selectable : 'single',
            autoSync : true,
            columnsMenu : true,
            pageable : {input : true, numeric : false},
            columns : columns,
            change : function(e) {
                var selected = $.map(this.select(), function(itm) {
                    return $(itm).find("input[type=checkbox]").val();
                });
                $this.selectRowId = selected[0];
            },
            detailInit: function(e) {
                var vl = isEmpty(e.data.vehicleList) ? new Array() : e.data.vehicleList.split(',');
                var sl = isEmpty(e.data.spaceList) ? new Array() : e.data.spaceList.split(',');
                var div = '<div class="detail-info-div">';
                div += '    <div class="detail-info-vehicle">';
                div += '        <div class="detail-info-vehicle-count"><div class="title">车辆</div><div class="value">'+(vl.length==0?0:vl.length-1)+'</div></div>';
                div += '        <div class="detail-info-vehicle-list">';
                if (vl.length == 0) {
                    div += '暂无车辆';
                } else {
                    div += '<ul>';
                    for (var i = 0; i < vl.length-1; i++) {
                        div += '<li>'+vl[i]+'</li>';
                    }
                    div += '</ul>';
                }
                div += '        </div>';
                div += '    </div>';
                div += '    <div class="detail-info-parking-space">';
                div += '        <div class="detail-info-vehicle-count"><div class="title">车位</div><div class="value">'+(sl.length==0?0:sl.length-1)+'</div></div>';
                div += '        <div class="detail-info-vehicle-list">';
                if (sl.length == 0) {
                    div += '暂无车位';
                } else {
                    div += '<ul>';
                    for (var i = 0; i < sl.length-1; i++) {
                        div += '<li>'+sl[i]+'</li>';
                    }
                    div += '</ul>';
                }
                div += '        </div>';
                div += '    </div>';
                div += '';
                div += '</div>';
                $('<div/>').appendTo(e.detailCell).html(div);
            },
            dataBound: function() {
                $this.$parent.find('.k-detail-cell').attr('colspan', 5);
                this.expandRow(this.tbody.find("tr.k-master-row"));
            }
        });
    },
    editWindow : function(e){
        var $this = this;
        if (!isEmpty(e)) $this = $(e.target).data('that');
        if (!isEmpty($this.selectRowId)) {
            JsonpAjax(baseApiPath + 'parking_space/detail?code='+$this.selectRowId, undefined, 'json', function(res){
                if (!isEmpty(res)) {
                    $this.entity = res;
                    $this.ownerCode = $this.selectRowId;
                    $this.openWindow();
                }
            }, false);
        } else {
            notification({title : '提示', message : '请选择需要修改的内容'}, 'info');
        }
    },
    openWindow : function(title) {
        title = title || "车位信息";
        var $this = this;
        $this.editDiv = $this.sysUtils.openWindow($this.editDiv, {
            title : title,
            height : 680,
            width : 1000,
            resizable : false,
            content : 'views/parkLotCenter/parkingLotInfo/parking_lot_edit.html',
        }, true)
    },
    findData : function (e) {
        var $this = this;
        if (!isEmpty(e)) $this = $(e.target).data('that');
        var dataT = {jsonParam:JSON.stringify($this.sysUtils.getBindDocJson($this.models.bindFieldArray))};
        var dataSource = $this.sysUtils.getDataSource({url : baseApiPath + 'parking_space/list', data : dataT, dataType : 'odata'});
        var grid = $this.$parent.find('#parking_lot_list').data('kendoGrid');
        grid.setDataSource(dataSource);
    }
}